/**
 * Created by gongxun on 17/1/19.
 */

var NnTableRoomEndLayer = GameCCSBase.extend({
    Panel_players: null,
    ctor: function () {
        this._super();
        this.loadCocostudio("res/ccs/NnTableRoomEnd.json");
        mo.gameHelper.fixIosText(this.ccsNode);
        this.Panel_players = [];

        mo.seekWidgetByName(this.ccsNode, "Button_guanbi").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                // mo.nnTable.quitNnGame();
                if (mo.sceneHelper.currentLayerName == "login"){
                    //在登录界面就会带登录
                    mo.sceneHelper.intoLoginLayer();
                }else{
                    mo.sceneHelper.runSceneHall();
                }
                mo.sceneHelper.closeEndInfoLayer();
            }
        }, this);

        mo.seekWidgetByName(this.ccsNode, "Button_ok").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                mo.gameHelper.onWxShareFriend(mo.gameHelper.projectName + "棋牌-牌局结算-斗牛", "我完成了一个" + mo.gameHelper.projectName + "棋牌的牌局，这个是结算截图！");
            }
        }, this);

        this.loadUI();

        this.setVisible(false);

        mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_gameName"), mo.gameHelper.projectName + "卡五星");
    },

    onEnter : function(){
        this._super();
    },

    onExit : function(){
        mo.eventHelper.remove(this);
        this._super();
    },

    loadUI: function () {
        for (var i = 0; i < 5; i++) {
            var Panel_player = mo.seekWidgetByName(this.ccsNode, "Image_player" + i);
            this.Panel_players.push(Panel_player);
        }
    },

    setRoomEndInfo : function(juEndedEvent){

        //房间规则
        var ruleJson = JSON.parse(juEndedEvent.CfgJson);
        var rule = "游戏规则：" + mo.gameHelper.getRuleConfigSummary(ruleJson, "nn", juEndedEvent.TableId);
        mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_rule"), rule);

        //时间
        var date = new Date(juEndedEvent.EndTime * 1000);
        // var time = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " ";
        // time += (date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds());
        mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_time"), mo.gameHelper.getTimeYYMMDD(date));

        //房间号
        var tableIdStr = "" + juEndedEvent.TableId;
        mo.seekWidgetByName(this.ccsNode, "AtlasLabel_roomId").setString(tableIdStr);

        //局数
        var totalJu = mo.nnTable.mGameConfig.maxpan;
        var jushuStr = "" + (parseInt(totalJu) - juEndedEvent.LeftPlayTimes) + "/" + totalJu;
        mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_jushu"), jushuStr);

        //大赢家
        var bigWinIndex = 0;
        var tempSocore = 0;

        //按照分数排序
        var juInfos = juEndedEvent.Ju.slice(0);
        juInfos.sort(function(a, b){
            return b.Score - a.Score;
        });

        //玩家数量
        var playerCount = 0;

        //用户信息
        for (var seat = 0; seat < 5; seat++){
            var juInfo = juInfos[seat];
            var player = this.Panel_players[seat];
            if (juInfo == null){
                player.setVisible(false);
                continue;
            }
            playerCount++;

            //nick
            mo.gameHelper.setTextString(mo.seekWidgetByName(player, "Text_nick"), mo.gameHelper.getUserNickFilterForCount(juInfo.Nick, 4));

            //uid
            mo.gameHelper.setTextString(mo.seekWidgetByName(player, "Text_uid"), "id:" + juInfo.Uid);

            //head
            mo.gameHelper.loadImageForUrl(mo.seekWidgetByName(player, "Image_head"), juInfo.FaceUrl);

            //房主
            mo.seekWidgetByName(player, "Image_create").setVisible(juInfo.Uid == juEndedEvent.CreateUid);

            //score
            var Fnt_score = mo.seekWidgetByName(player, "Fnt_score");
            var scoreStr = juInfo.Score;
            if (scoreStr >= 0){
                Fnt_score.setFntFile("res/ccs/commonEnd/winFnt.fnt");
                scoreStr = "+" + scoreStr;
            }else {
                Fnt_score.setFntFile("res/ccs/commonEnd/loseFnt.fnt");
            }
            Fnt_score.setString(scoreStr);

            //是否赢了
            mo.seekWidgetByName(player, "Image_isWin").loadTexture(juInfo.Score >= 0 ? "res/ccs/commonEnd/img_win.png" : "res/ccs/commonEnd/img_lose.png");

            //info
            var infoStr = "";
            infoStr += ("通杀次数:        " + juInfo.AllWinTimes);
            infoStr += ("\n通赔次数:        " + juInfo.AllLostTimes);
            infoStr += ("\n牛牛次数:        " + juInfo.NNTimes);
            infoStr += ("\n胜利局数:        " + juInfo.WinTimes);
            mo.gameHelper.setTextString(mo.seekWidgetByName(player, "Text_info"), infoStr);

            //大赢家计算
            if (juInfo.Score > tempSocore){
                tempSocore = juInfo.Score;
                bigWinIndex = seat;
            }
        }

        this.onChangePlayerPos(playerCount);

        //大赢家
        for (var i = 0; i < 5; i++){
            var player = mo.seekWidgetByName(this.ccsNode, "Image_player" + i);
            var Image_bigWinner = mo.seekWidgetByName(player, "Image_bigWinner");
            Image_bigWinner.setVisible(i == bigWinIndex);
        }

        //礼券
        var Text_liquan = mo.seekWidgetByName(this.ccsNode, "Text_liquan");
        Text_liquan.setVisible(false);
        for (var i = 0; i < juEndedEvent.Ju.length; i++){
            if (juEndedEvent.Ju[i].Uid == mo.gameUserInfo.uid){
                if (juEndedEvent.Ju[i].Coupon != null && juEndedEvent.Ju[i].Coupon != 0){
                    mo.gameHelper.setTextString(Text_liquan, "+" + juEndedEvent.Ju[i].Coupon);
                    Text_liquan.setVisible(true);
                }
            }
        }

        //结束原因
        var Text_exitInfo = mo.seekWidgetByName(this.ccsNode, "Text_exitInfo");
        if (juEndedEvent.EndFlag == 0){
            mo.gameHelper.setTextString(Text_exitInfo, "自然结束");
        }else if (juEndedEvent.EndFlag == 1){
            mo.gameHelper.setTextString(Text_exitInfo, "用户投票结束");
        }else if (juEndedEvent.EndFlag == 2){
            mo.gameHelper.setTextString(Text_exitInfo, "生死战结束,生死战封顶:" + ruleJson.lod);
        }
    },

    onChangePlayerPos : function(playerCount){
        var Panel_player = mo.seekWidgetByName(this.ccsNode, "Panel_player");
        var width = 200;
        var parentWidth = Panel_player.getContentSize().width;
        if (parentWidth < width * playerCount){
            //如果大小不够 就只能缩放了
            Panel_player.setContentSize(cc.size(width * playerCount, 0));
            Panel_player.setScale(parentWidth / (width * playerCount));

            parentWidth = width * playerCount;
        }

        var jiange = (parentWidth - width * playerCount) / (playerCount + 1);

        for (var i = 0; i < playerCount; i++){
            this.Panel_players[i].setPositionX(jiange * (i + 1) + i * width + width / 2);
        }
    }
});